<?php
namespace App\Http\Lib;

/**
* 工具类函数集合
*/
class Util
{
  
  public function __construct(){
  }

  // app标识的tag
  # => string()
  public static function appTag($name){
    return env('APP_NAME') . '_' . $name;
  }

  public static function cookieTag($name){
    return self::appTag($name . '@cookie');
  }

  public static function genUrl($url, array $params = []){
    if(!$params)
      return $url;
    
    if(strpos($url, '?') === false){
      $url .= '?';
    }else{
      $url .= '&';
    }

    $url .= http_build_query($params);

    return $url;
  }

  // => true | false
  public static function isMobile() {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
      return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset($_SERVER['HTTP_VIA'])) {
      // 找不到为flase,否则为true
      return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
      $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel',
      'lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi',
      'openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger');
      // 从HTTP_USER_AGENT中查找手机浏览器的关键字
      if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
        return true;
      }
    }
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
      // 如果只支持wml并且不支持html那一定是移动设备
      // 如果支持wml和html但是wml在html之前则是移动设备
      if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') ===
      false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
        return true;
      }
    }
    return false;
  }

  public static function filt_script($str){
    if(!$str)
      return $str;

    return str_ireplace(
      ['<script', '<style', '<link', '</script', '</style', '<img', '<svg',
      '<iframe', '<form', '<input', '<audio', '<video', '<source', '</source',
      '<select', '<keygen', '<textarea', '</textarea', '\u', '<s', '</s'], 
      ['<noscript', '<nostyle', '<nolink', '</noscript', '</nostyle', '<noimg', '<nosvg', 
      '<noiframe', '<noform', '<noinput', '<noaudio', '<novideo', '<nosource', '</source',
      '<noselect', '<nokeygen', '<notextarea', '</notextarea', 'u', '<nos', '</nos'], $str);
  }

  public static function filt_html($str){
    if(!$str)
      return $str;

    return htmlentities($str, ENT_NOQUOTES);
  }

  public static function clean_xss(&$str){
    if(empty($str))
      return $str;

    $str = self::filt_script($str);
    $str = self::filt_html($str);

    return $str;
  }

  public static function html_to_plain($str){
    $str = preg_replace('/<\/p>/', "\r\n", $str);
    return preg_replace('/<.*?>/', '', $str);
  }

  // 使字符串（文字段）变短(过滤空格换行，并且超过长度$limit的显示...)
  public static function shorten($str, $limit = 110, $replaceStr = '...'){
    $pattern = ['/ /','/　/', '/\r/', '/\n/', '/<.*?>/', '/&nbsp;/'];
    $replace = '';
    $str = preg_replace($pattern, $replace, $str);
    // 
    $str = preg_replace('/.*?回复意见：/', $replace, $str);

    if(iconv_strlen($str, 'utf-8') > $limit)
      $str =  iconv_substr($str, 0, $limit, 'utf-8') . $replaceStr;

    return $str;
  }

}






